package org.yankun.exam4j.web.controller.paper;

import java.util.ArrayList;
import java.util.HashMap;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import org.yankun.exam4j.core.paper.service.PaperService;
import org.yankun.exam4j.core.question.service.QuestionService;
import org.yankun.exam4j.core.result.service.ResultService;
import org.yankun.exam4j.entity.Paper;
import org.yankun.exam4j.entity.PaperQuestion;
import org.yankun.exam4j.entity.Question;
import org.yankun.exam4j.entity.Result;
import org.yankun.exam4j.entity.User;
import org.yankun.exam4j.security.user.service.UserService;
import org.yankun.exam4j.session.SessionManager;
import org.yankun.exam4j.session.UserSessionData;


@Controller("PaperController")
@RequestMapping("/paper")
public class PaperController {
	
	@Resource
	private PaperService paperService;
	
	@Resource
	private QuestionService questionService;
	
	@Resource
	private ResultService resultService;
	
	@Resource
	private UserService userService;
	
	
	@RequestMapping(value="{paperId}/start.do")
	public ModelAndView start(HttpServletResponse response,HttpServletRequest request,@PathVariable("paperId") Integer paperId){
		
		HttpSession session=request.getSession();
		UserSessionData usd = SessionManager.getSessionData(session);
		User user = userService.getUserById(usd.getUserId());
		
		Result result = resultService.getResultbyPaperIdAndUserId(paperId, user.getUid());
		Paper paper = paperService.getPaperById(paperId);
		
		if(result == null){
			result = new Result();
			result.setPaper(paper);
			result.setFinished(false);
			result.setUser(user);
			resultService.save(result);
		}
		
		session.setAttribute("resultId", result.getUid());
		Map<String, Object> map=new HashMap<String, Object>();
		
		List<PaperQuestion> paperQuestionList = questionService.getPaperQuestionByPaper(paperId);
		

		Map<String,List<Question>> questionMap = null;
		
		if(null != paperQuestionList){
			questionMap = new LinkedHashMap<String,List<Question>>();
			
			List<Question> tempQuestion = null;
			
			int typeValue = 0;
			for(int i=0,n=paperQuestionList.size();i<n;i++){
				Question question = paperQuestionList.get(i).getQuestion();

				String typeString = String.valueOf(question.getQuestionTypeConfig().getTypeName());
				if(typeValue != question.getQuestionTypeConfig().getUid()){
					tempQuestion = new ArrayList<Question>();
					questionMap.put(typeString, tempQuestion); //加入
				}else{
					tempQuestion = questionMap.get(typeString);
				}
				
				tempQuestion.add(question);		//增加至临时List
				typeValue = question.getQuestionTypeConfig().getUid();
				
			}
			
		}

		
		map.put("questionMap", questionMap);
		return new ModelAndView("/paper/paper",map);
	}
	
}